import { Injectable } from '@angular/core';
import { ApiService } from '@core/services/http/api.service';
import { ApesMessageService } from '@shared/apes/message';
import { DownloadService } from '@core/services/tool/download.service';
import { ImportService } from '@core/services/tool/import.service';
import { HtmlToCanvasService } from '@core/services/tool/htmlToCanvas.service';
import { LinkService } from '@core/services/tool/link.service';
import { MenuService } from '@core';
import { UserAuthService } from '@core/services/user-auth.service';
import { ReuseTabService } from '@shared/apes/reuse-tab';
import { ActivatedRoute } from '@angular/router';
import { Events } from '@core/services/events/Events';

export interface ButtonType {
  id: string;
  action: string;
  option: any
}

@Injectable({
  providedIn: 'root',
})
export class ButtonService {

  constructor(private apiService: ApiService,
              private msg: ApesMessageService,
              private downloadService: DownloadService,
              private importService: ImportService,
              private h2c: HtmlToCanvasService,
              private linkService: LinkService,
              private menuService: MenuService,
              private userAuthService: UserAuthService,
              protected reuseTabService: ReuseTabService,
              protected route: ActivatedRoute,
              protected events: Events,
  ) {
  }

  clickButton(btn: ButtonType) {
    switch (btn.action) {
      case 'download':
        this.onDownload(btn.option);
        break;
      case 'import':
        this.onImport(btn.option);
        break;
      case 'h2c':
        this.onH2C(btn.option);
        break;
      case 'link':
        this.onLink(btn.option);
        break;
      // case 'bottom':
      //   break;
    }
  }

  private onDownload(param) {
    this.downloadService.startDownloadData(param);
  }

  private onImport(param) {
    this.importService.startImportData(param);
  }

  private onH2C(param) {
    this.h2c.htmlToCanvas(param, (file) => {
      // console.log(file);
      let resData = {
        id : param.id,
        name : param.name,
        file : file,
      }
      this.events.publish('h2c', resData);
    });
  }

  private onLink(param) {
    this.linkService.startLinkData(param);
  }


  getMenuLog(url, type) {
    // type : search 搜索框入口 | common 常用 | assembly 组合 | menu 菜单入口 | link 详情 | todo 待办入口
    console.log(url);
    let userInfoModel = this.userAuthService.getUserInfoObj();
    let userId = userInfoModel.userAccount.userId;
    let userName = userInfoModel.userAccount.name;
    let skipList = this.menuService.getPathByUrl(url);
    let skipItem = skipList[skipList.length - 1];
    let curURL = this.reuseTabService.getUrl(this.route.snapshot);
    let list = this.menuService.getPathByUrl(curURL);
    let currentItem = list[list.length - 1];
    let api = 'fn.menuStatistics.add';
    if (!skipItem) {
      skipItem = {
        link: url,
        id: 0,
        text: '特殊',
      };
    }
    if (!currentItem) {
      currentItem = {
        link: curURL,
        id: 0,
        text: '特殊',
      };
    }

    let setData = {
      formId: skipItem.link,
      menuId: skipItem.id,
      menuName: skipItem.text,
      sourceFormId: currentItem.link,
      sourceMenuId: currentItem.id,
      sourceMenuName: currentItem.text,
      type: type,
      userId: userId,
      userName: userName,
    };

    this.apiService.post(api, setData).subscribe((body) => {
      // console.log('getPin', body);
    });
  }

}
